// 注册与登录

import { reqGetCode, reqRegister, reqLogin, reqUserInfo, reqLogout } from "@/api"

const state = {
  userInfo: {}
}
const mutations = {
  USERINFO(state, userinfo) {
    state.userInfo = userinfo
  },
  LOGOUT(state) {
    // 清除本地token
    localStorage.clear()
    // 清除仓库用户数据
    state.userInfo = {}

  }
}
const actions = {
  // 获取验证码
  async getCode() {
    let phone = arguments[1]
    return await reqGetCode(phone)
  },
  // 注册
  async userRegister() {
    let { phone, password, code } = arguments[1]
    let params = { phone, password, code }
    return await reqRegister(params)
  },
  // 登录
  userLogin() {
    let { phone, password } = arguments[1]
    return reqLogin(phone, password)
  },
  // 获取用户信息
  getUserInfo({ commit }) {
    reqUserInfo().then(res => {
      commit('USERINFO', res.data)
    })
  },
  // 退出登录
  userLogout({ commit }) {
    reqLogout().then(() => {
      commit('LOGOUT')
    })
  }
}
const getters = {}

export default {
  state, mutations, actions, getters
}